<?php
namespace app\admin\controller;

use app\admin\model\User as UserModel;
use app\admin\service\UserService;
use app\admin\traits\Result;
use think\Db;

class User extends Common
{
    
    //会员列表
    public function userList()
    {
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            
            $list = UserModel::withSearch(['name'], ['name' => $data['key']])->order('uid desc')
                //->hidden(['password'])
                ->paginate($data['limit'], false, ['query' => $data]);
                //dump(UserModel::getlastsql());die;
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
                //$user_date[$key]['title'] = $val->group_titles;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
    }  
    
    /**
     * 添加、编辑会员
     */
    public function userEdit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['uid']) {
                //编辑
              
                $res = UserService::edit($data);
                return $res;
            } else {
                //添加
                $data = UserService::add($data);
                return $data;
                //$this->error('参数错误');
            }
        } else { 
            $uid = $this->request->get('uid', 0, 'intval');
            if ($uid) {
                $list = UserModel::where('uid', '=', $uid)->hidden(['password'])->find();
                $this->assign('list', $list);
            }
            return $this->fetch();
        }
    }
    
    //会员删除
    public function userDelete()
    {
        $uid = $this->request->param('uid', 0, 'intval');
        if ($uid) {
           
                
                $res = UserModel::where('uid='.$uid)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
    }
    
    //会员认证记录
    public function userAuthen(){
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];
            $map = [];
            if(!empty($data['key'])){
                $key = $data['key'];
                //$map['idcard_name'] = array('like',"%$key%");
                $map[] = ['idcard_name','like',"%".$key."%"];
            }
            $uid = input('uid',0,'intval');
            if(!empty($uid)){
                $map[] = ['uid','=',$uid];
            }
            //$list =     db('user_authen')->where('idcard_name','like',"%$key%")->paginate($data['limit'], false, ['query' => $data]);
            $list = db('user_authen')->where($map)->order('rid desc')->paginate($data['limit'], false, ['query' => $data]);
          //  echo Db::name('user_authen')->getLastSql();die;
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        $uid = input('uid',0,'intval');
        $this->assign('uid',$uid);
        return $this->fetch();
    }
    
    //会员认证删除
    public function authenDel()
    {
        $rid = $this->request->param('rid', 0, 'intval');
        if ($rid) {
                $res = db('user_authen')->where('rid='.$rid)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
    }

    public function daoru(){die;
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['fileurl'])){
                $this->error('请上传文件');
            }
            $config   = ['path' => '.'.dirname($data['fileurl'])];
            $excel  = new \Vtiful\Kernel\Excel($config);
            $data = $excel->openFile(basename($data['fileurl']))->openSheet()->getSheetData();
            if(empty($data)){
                $this->error('文件没有内容');
            }
            if(count($data)<2){
                $this->error('文件没有内容');
            }
            unset($data[0]);

            $array = [];
            foreach($data as $k=>$v){
                if(empty($v[0]) || empty($v[1]) || empty($v[2])){
                    $this->error('文件内容未填写完整');
                }
                $v[0] = trim($v[0]);
                $uid = Db::name('user')->where('username',$v[0])->value('uid');
                if(!empty($uid)){
                    $this->error('文件中:"'.$v[0].'已存在');
                }
                $v[1] = trim($v[1]);
                $v[2] = trim($v[2]);
                $pass_key = self::str_rand(10);
                $password = md5($v[1].$pass_key);
                $array[$k] = ['username'=>$v[0],'name'=>$v[2],'password'=>$password,'pass_key'=>$pass_key,'create_time'=>time()];
            }
            $flag = Db::name('user')->data($array)->limit(100)->insertAll();
            if($flag){
                $this->success('导入成功', url('/admin/user-list'));
            }else{
                $this->error('导入失败');
            }

        } else {
            return $this->fetch();
        }
    }

    /**
     * 生成指定长度随机字符串
     * @param int $length
     * @param string $char
     * @return bool|string
     */
    static function str_rand($length = 32, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
        if(!is_int($length) || $length < 0) {
            return false;
        }
        $string = '';
        for($i = $length; $i > 0; $i--) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }
        return $string;
    }
    
}

?>